package com.it.emall.mapper;

import com.it.emall.domain.AdminUser;
import com.it.emall.domain.Comments;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface CommentsMapper {




    /**
     * 获取用户的头像和用户名
     * @param userId 用户ID
     * @return 用户的头像和用户名
     */
    @Select("SELECT username, avatar FROM users WHERE user_id = #{userId}")
    @Results({
            @Result(property = "username", column = "username"),
            @Result(property = "avatar", column = "avatar")
    })
    AdminUser getUserInfoById(int userId);

    /**
     * 添加评论
     * @param userId 用户ID
     * @param recipeId 食谱ID
     * @param content 评论内容
     * @param avatar 用户头像链接
     * @param username 用户名
     * @return 插入记录数
     */
    @Insert("INSERT INTO comments (user_id, recipe_id, username, content, avatar, created_at) " +
            "VALUES (#{userId}, #{recipeId},#{username}, #{content}, #{avatar}, NOW())")
    int addComment(@Param("userId") int userId, @Param("recipeId") int recipeId,@Param("username") String username,
                   @Param("content") String content, @Param("avatar") String avatar);

    /**
     * 根据食谱ID获取所有评论
     * @param recipeId 食谱ID
     * @return 食谱的评论列表
     */
    @Select("SELECT * FROM comments WHERE recipe_id = #{recipeId} ORDER BY created_at DESC")
    List<Comments> findCommentsByRecipeId(int recipeId);

    /**
     * 根据评论ID获取评论
     * @param commentId 评论ID
     * @return 评论内容
     */
    @Select("SELECT * FROM comments WHERE id = #{commentId}")
    Comments findCommentById(int commentId);

    /**
     * 删除评论
     * @param commentId 评论ID
     * @return 删除记录数
     */
    @Delete("DELETE FROM comments WHERE id = #{commentId}")
    int deleteComment(int commentId);
}
